package org.iweb.html.impl.basic;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.iweb.html.api.AbstractHtmlElement;

public class Head extends AbstractHtmlElement {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1902025305390976229L;
	private Title title;
	private String context;

	public Head(String context) {
		this.context = context;
		if (this.context == null)
			this.context = "";
		removeAttr("id");
		removeAttr("class");
		title = new Title("example");
		append(title);// .append(new
						// DefaultScript(rootPath+"/js/jquery-1.7.2.min.js")).append(new
						// DefaultScript(rootPath+"/js/jquery-ui-1.8.20.custom.min.js")).append(new
						// DefaultStyle(rootPath+"/css/style.css"));//.append(new
						// DefaultStyle("css/ui-lightness/jquery-ui-1.8.20.custom.css"));
		Meta m=new Meta();
		m.addAttr("http-equiv", "Content-Type");
		m.addAttr("content", "text/html; charset=utf-8");
		
		append(m);
		//loadDefaults();
	}

	public Head setTitle(String value) {
		Title tmp=new Title(value);
		tmp.setId(title.getId());
		replace(title,tmp);
		title=tmp;
		return this;
	}

	@SuppressWarnings("resource")
	protected void loadDefaults() {

		try {
			URI jarUrl = this.getClass().getProtectionDomain().getCodeSource()
					.getLocation().toURI();
			//System.out.println(jarUrl);

			JarFile jarFile = new JarFile(jarUrl.getPath());

			String webPath = jarUrl.getPath().replaceFirst("/WEB-INF/lib/jq-framework.jar","");
			//System.out.println(webPath);

			Enumeration<JarEntry> enum1 = jarFile.entries();
			while (enum1.hasMoreElements()) {
				JarEntry entry = (JarEntry) enum1.nextElement();
				String name = entry.getName();
			
				if (name.startsWith("resources/css/") || name.startsWith("resources/js/")) {
					//System.out.println(name);					

					name=name.replaceAll("resources", "");
					File efile = new File(webPath, name);
					if (entry.isDirectory()) {
						if (!efile.exists())
							efile.mkdirs();
					} else {						
						try {
							InputStream in = new BufferedInputStream(
									jarFile.getInputStream(entry));
							OutputStream out = new BufferedOutputStream(
									new FileOutputStream(efile));
							byte[] buffer = new byte[2048];
							for (;;) {
								int nBytes = in.read(buffer);
								if (nBytes <= 0)
									break;
								out.write(buffer, 0, nBytes);
							}
							out.flush();
							out.close();
							in.close();
						} catch (Exception e) {
							e.printStackTrace();
						}
						
						if (name.startsWith("/css") && name.endsWith(".css"))
							append(new DefaultStyle(context + name));
						if (name.startsWith("/js") && name.endsWith(".js"))
							append(new DefaultScript(context + name));
					}										
				}			
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static class DefaultScript extends Script {

		/**
		 * 
		 */
		private static final long serialVersionUID = -5013084531884005332L;

		public DefaultScript(String jsFilePath) {
			addAttr("src", jsFilePath);
			removeAttr("class");
		}

		@Override
		public String getTag() {
			return "script";
		}

	}

	public static class DefaultStyle extends Style {

		/**
		 * 
		 */
		private static final long serialVersionUID = -5225760799663625046L;

		public DefaultStyle(String cssFilePath) {
			addAttr("href", cssFilePath);
			removeAttr("class");
		}

		@Override
		public String getTag() {
			return "link";
		}

	}

	public class Title extends AbstractHtmlElement {

		/**
		 * 
		 */
		private static final long serialVersionUID = 2386962889056352252L;

		public Title(String value) {
			super(value);
			removeAttr("id");
			removeAttr("class");
		}

		@Override
		public String getTag() {
			return "title";
		}

	}
	
	public static class Meta extends AbstractHtmlElement{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 4777880717775763330L;


		public Meta() {
			removeAttr("id");
			removeAttr("class");			
		}
		
		
		@Override
		public String getTag() {
			return "meta";
		}
	}
	
	
	public Head addStyleFile(String path){
		append(new DefaultStyle(path));
		return this;
	}
	
	public Head addScriptFile(String path){
		append(new DefaultScript(path));
		return this;
	}

}
